package cz.possoft.client.four.gui.components;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import cz.possoft.client.four.gui.panel.FourPanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.serializable.generic.Location;

/**
  * Listener nastava pri pohybu mysi nad sachovnici hry ctyri-v-rade
  *
  * @author mara  
  */
public class FourMouseMotionListener extends MouseMotionAdapter {

	   public void mouseMoved(MouseEvent me) {
		   	if (!(PanelHolderUtils.getCurrentGamePanel() instanceof FourPanel)) {
	    		return;
	    	}    		   
	        if ((PanelHolderUtils.getCurrentGamePanel().getGame().isActive()) && (PanelHolderUtils.getCurrentGamePanel().getGame().jsemNaTahu())) {
	            Location location = getMouseLocation(me);  
	            ((FourPanel)PanelHolderUtils.getCurrentGamePanel()).updateForPlaceWhereIsMouse(location);	            
	        }        
	    }

	    // vraci umisteni kde se prave nachazi mys ([0..7, 0..7])
	    // pro y-souradnici vraci vzdy horni radu.
	    public static Location getMouseLocation(MouseEvent me) {
	        int xx = me.getX();
	        int yy = me.getY();
	        if ((xx < 5) || (xx > 443) || (yy < 5) || (yy > 443)) {
	            return new Location(-1, -1);
	        } else {        	
	        	int xCil = (xx - 4) / 63;
	        	// oznacena je vzdy horni rada
	        	int yCil = 0;	        	     	
	        	// navrat lokace
	            return new Location(xCil, yCil);
	        }
	    }	
	
}
